<template>
  <div>
    <Category></Category>
    <el-card shadow="always" style="margin: 20px 0">
      <!-- 场景一:spu列表 -->
      <div v-show="scene == 0">
        <el-button type="primary" size="default" :icon="Plus"
          >添加SPU</el-button
        >
        <el-table border stripe style="margin: 20px 0" :data="spuList.records">
          <el-table-column label="序号" type="index" align="center" width="80">
          </el-table-column>
          <el-table-column label="SPU名称" prop="spuName"> </el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column label="操作">
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                :icon="Plus"
                title="添加SKU"
              ></el-button>
              <el-button
                type="warning"
                size="small"
                :icon="Edit"
                title="修改SPU"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                size="small"
                :icon="InfoFilled"
                title="查看SKU"
              ></el-button>
              <el-button
                type="danger"
                size="small"
                :icon="Delete"
                title="删除SPU"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          @current-change="getSpuList"
          @size-change="changeSize"
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7]"
          layout=" prev, pager, next, jumper,->, sizes,total"
          :total="spuList.total"
          background
          :pager-count="7"
        >
        </el-pagination>
      </div>
      <!-- 场景二:添加/修改spu -->
      <Spu v-show="scene == 1" ref="spuRef" @changeScene="changeScene"></Spu>
      <!-- 场景三:添加sku -->
      <Sku v-show="scene == 2"></Sku>
    </el-card>
  </div>
</template>

<script setup lang="ts">
// 引入icon图标
import {
  Check,
  Delete,
  Edit,
  Message,
  Search,
  Star,
  Plus,
  InfoFilled,
} from "@element-plus/icons-vue";
import { ref, watch } from "vue";
import useCategoryStore from "@/stores/category";
import { reqSpuList } from "@/api/product/spu/spu";
// 引入子组件
import Spu from "./addOrUpdateSpu.vue";
import Sku from "./addSku.vue";
// 引入仓库
let categoryStore = useCategoryStore();
// 定义响应式数据
let currentPage = ref<number>(1);
let pageSize = ref<number>(3);
// 定义服务器返回的数据
let spuList = ref({});
// 定义场景
let scene = ref<number>(0);
// 拿到子组件spu
let spuRef = ref<any>();

// 监听三级分类id的变化,发生变化就获取页面数据
watch(
  () => categoryStore.c3Id,
  () => {
    // 三级分类下的id不能为空
    if (categoryStore.c3Id == "") return;
    getSpuList();
  }
);

const getSpuList = async (pager: number = 1) => {
  currentPage.value = pager;
  let result = await reqSpuList(
    currentPage.value,
    pageSize.value,
    categoryStore.c3Id
  );
  console.log(result);
  spuList.value = result;
};

// 点击下拉框一页展示几条数据的回调
const changeSize = (limit: number) => {
  pageSize.value = limit;
  // 再次获取数据
  getSpuList();
};

// 修改spu的回调
const updateSpu = (row) => {
  // 切换场景
  scene.value = 1;
  // 拿到子组件实例,调用子组件的方法
  spuRef.value.getSpuData(row);
};

// 自定义事件
const changeScene = (params) => {
  // 此处有一个小 bug 待解决:即子组件点击取消之后切换场景,
  // 此时场景一的页面是在当前页还是返回第一页(即是否需要重新渲染)
  scene.value = params;
};
</script>

<style scoped>
</style>